//
//  PopoverPresentationController.swift
//  weibo-new
//
//  Created by mac on 2017/3/27.
//  Copyright © 2017年 gjx. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    lazy var corverView: UIView = UIView()
    var presentedFrame: CGRect = CGRect.zero

    // 修改被present出的控制器view尺寸
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        presentedView?.frame = presentedFrame
        
        // 添加遮盖view
        setupCoverView()
    }
}

// MARK:-- UI
extension PopoverPresentationController {
    func setupCoverView() {
        corverView.frame = containerView?.bounds ?? CGRect.zero
        containerView?.insertSubview(corverView, at: 0)
        corverView.backgroundColor = UIColor(white: 0, alpha: 0.4)
        // 添加手势监听
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(corverViewTapped))
        corverView.addGestureRecognizer(tapGesture)
    }
}

extension PopoverPresentationController {
    @objc fileprivate func corverViewTapped() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
